package com.dhecp.project.hr.service.impl;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.dhecp.common.core.domain.AjaxResult;
import com.dhecp.core.constant.DataFiltering;
import com.dhecp.core.utils.PageUtils;
import com.dhecp.framework.util.ShiroUtils;
import com.dhecp.project.common.domain.DataBaseMessage;
import com.dhecp.project.hr.domain.AttendanceWorkDateSet;
import com.dhecp.project.hr.mapper.AttendanceWorkDateSetMapper;
import com.dhecp.project.hr.service.AttendanceWorkDateSetService;
import com.dhecp.project.system.service.SysDictionaryForFieldService;

/**
 * 工作时间设定  业务实现层
 * @author DH-lwl
 * @version 2020年5月22日
 */
@Service
public class AttendanceWorkDateSetServiceImpl implements AttendanceWorkDateSetService {
	
	@Autowired
	private AttendanceWorkDateSetMapper workDateSetMapper;

	@Autowired
	private SysDictionaryForFieldService dictionaryForFieldService;

	/**
	 * 查询所有信息
	 * @param params
	 * @return List-AttendanceWorkDateSet
	 * @author lwl
	 * @throws Exception 
	 */
	@Override
	public List<AttendanceWorkDateSet> queryAll(Map<String, Object> params)
			throws Exception {
		// TODO Auto-generated method stub
		params.put(DataFiltering.DATA_WHERE, dictionaryForFieldService.getWhereStr(params));	//常规查询条件SQL语句
		if (params.get("field") == null || params.get("field") == ""){
			String orderBy = "dWorkDate asc";
			PageUtils.startPageOrder(orderBy);
		}else{
			PageUtils.startPage();	
		}
		return workDateSetMapper.findAll(params);
	}
	
	/**
	 * 通过guid查询单条记录
	 * @param guid
	 * @return 实体AttendanceWorkDateSet
	 * @author lwl
	 */
	@Override
	public AttendanceWorkDateSet queryWorkDateSetByGuid(String guid) {
		return workDateSetMapper.findWorkDateSetByGuid(guid);
	}

	/**
	 * 修改为上班时间
	 * @param attendanceWorkDateSetItems
	 * @return MessageModel
	 * @author lwl
	 */
	@Override
	public AjaxResult updateWorkday(Map<String, Object> params) {
		// TODO Auto-generated method stub
		List<AttendanceWorkDateSet> workdayList = JSON.parseArray(params.get("attendanceWorkDateSetItems").toString(), AttendanceWorkDateSet.class);
		if (workdayList == null || workdayList.size() == 0) {
            return AjaxResult.error("数据包格式有误，请检查！");
        }
		
		for (AttendanceWorkDateSet workday : workdayList) {
			workday.setUpdateUser(ShiroUtils.getUserId());	
			workDateSetMapper.updateWorkday(workday);
		}
		return AjaxResult.success("操作成功！");
	}

	/**
	 * 修改为休息时间
	 * @param attendanceWorkDateSetItems
	 * @return MessageModel
	 * @author lwl
	 */
	@Override
	public AjaxResult updateHoliday(Map<String, Object> params) {
		// TODO Auto-generated method stub
		List<AttendanceWorkDateSet> holidayList = JSON.parseArray(params.get("attendanceWorkDateSetItems").toString(), AttendanceWorkDateSet.class);
		if (holidayList == null || holidayList.size() == 0) {
            return AjaxResult.error("数据包格式有误，请检查！");
        }
		
		for (AttendanceWorkDateSet holiday : holidayList) {
			holiday.setUpdateUser(ShiroUtils.getUserId());
			workDateSetMapper.updateHoliday(holiday);
		}
		return AjaxResult.success("操作成功！");
	}

	/**
	 * 修改时间设定
	 * @param attendanceWorkDateSetItems
	 * @return MessageModel
	 * @author lwl
	 */
	@Override
	public AjaxResult update(Map<String, Object> params) {
		// TODO Auto-generated method stub
		List<AttendanceWorkDateSet> dateSetList = JSON.parseArray(params.get("attendanceWorkDateSetItems").toString(), AttendanceWorkDateSet.class);
		if (dateSetList == null || dateSetList.size() == 0) {
            return AjaxResult.error("数据包格式有误，请检查！");
        }
		
		for (AttendanceWorkDateSet dateSet : dateSetList) {
			dateSet.setUpdateUser(ShiroUtils.getUserId());		
			workDateSetMapper.update(dateSet);
		}
		return AjaxResult.success("操作成功！");
	}

	/**
	 * 一键生成
	 * @param userId,vYear
	 * @return AjaxResult
	 * @author DH-Xxj
	 */
	@Override
	public AjaxResult insertWorkDateSet(Map<String, Object> params) {
		// TODO Auto-generated method stub
		params.put("userid", ShiroUtils.getUserId());
		DataBaseMessage dataBaseMessage = workDateSetMapper.insertWorkDateSet(params);
		
		if ("0".equals(dataBaseMessage.getCode())){
			return AjaxResult.success(dataBaseMessage.getMsg());
		}else if ("301".equals(dataBaseMessage.getCode())){
			return AjaxResult.warn(dataBaseMessage.getMsg());
		}else if ("500".equals(dataBaseMessage.getCode())){
			return AjaxResult.error(dataBaseMessage.getMsg());
		}else{
			return AjaxResult.error("未知服务器错误，请重试。");
		}
	}

}
